package com.example.demo.controller;

import com.example.demo.model.Login;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by liulanhua on 2018/9/25.
 */
@Slf4j
@RestController
@RequestMapping(value = "/")
public class TestController {


    @GetMapping(value="")
    public ModelAndView index(ModelAndView mv) {
        mv.addObject("record", "this is index page");
        mv.setViewName("index");
        return mv;
    }

    @GetMapping(value="/loginPage")
    public ModelAndView loginPage(ModelAndView mv, String name) {
        mv.addObject("record", "this is login page");
        mv.setViewName("login");
        mv.addObject("name", name);
        return mv;
    }

    @PostMapping(value="/login", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ModelAndView login(ModelAndView mv, @RequestBody Login req) {
        mv.addObject("record", "this is user page");
        mv.setViewName("user");
        mv.addObject("req", req);
        return mv;
    }

    @GetMapping(value="/page1")
    public ModelAndView page1(ModelAndView mv, String name) {
        mv.setViewName("page1");
        mv.addObject("name", name);
        return mv;
    }

    @GetMapping(value="/page2")
    public ModelAndView page2(ModelAndView mv, String name) {
        mv.setViewName("page2");
        mv.addObject("name", name);
        return mv;
    }

    @PostMapping(value = "/fallbackUrl")
    public ResponseEntity<String> fallbackUrl(@RequestParam(value = "json") String json) {
        log.info("json>>>>>>{}", json);
        return new ResponseEntity<>(json, HttpStatus.OK);
    }




}
